$(document).ready(function() {
    if (typeof(Storage) !== "undefined")
    {

        $('.wd-loading').show();

        getfriendFacebook();
        var refresh = false;
        $("#act-refresh-friend").click(function() {
            if (refresh === false) {
                refresh = true;
                $('.wd-loading').show();
                $.post(localStorage.domain_app+"/webservice/facebook/refreshFriendList.php", {
                    access_token: localStorage.facebook_token,
                    facebook_id: localStorage.facebook_id,
                    user_id: localStorage.userID,
                }, function(result_2) {
                    refresh = false;
                    $("#act-invite-friend").show();
                    $('.wd-loading').hide();
                    getfriendFacebook();

                }, "json");
            }
        });
        //paging
        var killScroll = false;
        $(window).scroll(function() {

            if ($('#end_paging').val() == 0) {
                if ($(window).scrollTop() > ($(document).height() - $(window).height() - 100)) {
                    $('.wd-loading').show();
                    if (killScroll === false) {
                        killScroll = true;
                        $.post(localStorage.domain_app+"/webservice/guest/getFriendList.php", {
                            page: $('#page').val(),
                            user_id: localStorage.userID
                        }, function(res) {
                            $('#wd-list-friend').append(res.html);
                            $('#page').val(res.page);
                            $('#end_paging').val(res.end_paging);
                            checkAll();
                            killScroll = false;
                            $('.wd-loading').hide();
                        }, "json");
                    } else {
                        return false;
                    }
                }
            }
        });
        var invite = false;
        $('#act-invite-friend').click(function() {
            if (invite === false) {
                invite = true;
                var friend_list = new Array();
                var count = 0;
                $('.checkbox-invite-friend').each(function() {
                    if ($(this).is(':checked')) {
                        friend_list[count] = $(this).attr('rel');
                        count++;
                    }
                });
                $.post(localStorage.domain_app+"/webservice/guest/inviteFriend.php", {
                    user_id: localStorage.userID,
                    friend_list: friend_list
                }, function(res) {
                    if (res.error == 1) {
                        print_alert(res.msg);
                    } else {
                        print_alert(res.msg);
                        $('.wd-loading').show();
                        wd_redirect("guestList.html");
                        //getfriendFacebook();
                    }
                    invite = false;
                }, "json");
            }
        });
    }
});
function checkAll() {
    $('.checkall-friends').click(function() {
        var checkbox_all = $('#' + $(this).attr('for')).is(':checked');
        if (checkbox_all) {
            $('#wd-list-friend .regular-checkbox-addguest').removeAttr('checked');
        } else {
            $('#wd-list-friend .regular-checkbox-addguest').attr('checked', 'checked');
        }

    });
}
function getfriendFacebook() {
    $('#wd-checkall').removeAttr('checked');
    $.post(localStorage.domain_app+"/webservice/guest/getFriendList.php", {
        page: 1,
        user_id: localStorage.userID
    }, function(res) {

        if (res.error == 0) {
            $('#wd-list-friend').html(res.html);
            $('#page').val(res.page);
            $('#end_paging').val(res.end_paging);
            $('.wd-loading').hide();
            checkAll();
        } else {
            $("#act-invite-friend").hide();
            $('#wd-list-friend').html(res.html);
            $('.wd-loading').hide();
        }
    }, "json");
}
